package com.wm.web.action;

import java.util.List;
import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.SessionAware;

import com.wm.bean.Scriptorium;
import com.wm.bean.User;
import com.wm.comm.Constant;
import com.wm.comm.MD5;
import com.wm.service.AreaService;
import com.wm.service.CityService;
import com.wm.service.ScriptoriumService;
import com.wm.service.UserService;
import com.wm.service.web.PageList;
import com.wm.service.web.Pages;
import com.wm.web.servlet.UserCookie;
import com.wm.web.servlet.UserSession;

public class IndexAction extends BaseAction implements SessionAware{

	private static final Log logger = LogFactory.getLog(IndexAction.class);
	
	private Map session;

	public void setSession(Map session){
    	this.session = session;
    }
    
    private CityService cityService;
    private AreaService areaService;
    private ScriptoriumService scriptoriumService;
    
    public CityService getCityService() {
		return cityService;
	}

	public void setCityService(CityService cityService) {
		this.cityService = cityService;
	}

	public AreaService getAreaService() {
		return areaService;
	}

	public void setAreaService(AreaService areaService) {
		this.areaService = areaService;
	}

	public ScriptoriumService getScriptoriumService() {
		return scriptoriumService;
	}

	public void setScriptoriumService(ScriptoriumService scriptoriumService) {
		this.scriptoriumService = scriptoriumService;
	}
	
	public List areaList;
	public List scriptoriumList;
	private long sid;
	private long areaId;
	private int scrType;
	private int returnType;
	private String jsonString;

	public int getReturnType() {
		return returnType;
	}

	public void setReturnType(int returnType) {
		this.returnType = returnType;
	}

	public String getJsonString() {
		return jsonString;
	}

	public void setJsonString(String jsonString) {
		this.jsonString = jsonString;
	}
	
	public int getScrType() {
		return scrType;
	}

	public void setScrType(int scrType) {
		this.scrType = scrType;
	}

    public long getSid() {
		return sid;
	}

	public void setSid(long sid) {
		this.sid = sid;
	}

	public long getAreaId() {
		return areaId;
	}

	public void setAreaId(long areaId) {
		this.areaId = areaId;
	}
	
	public List getAreaList() {
		return areaList;
	}

	public void setAreaList(List areaList) {
		this.areaList = areaList;
	}

	public List getScriptoriumList() {
		return scriptoriumList;
	}

	public void setScriptoriumList(List scriptoriumList) {
		this.scriptoriumList = scriptoriumList;
	}
	
	private PageList pageList;
	
	public PageList getPageList() {
		return pageList;
	}

	public void setPageList(PageList pageList) {
		this.pageList = pageList;
	}	
	
	public String execute() {
		try {
			return this.executeMethod(this.getAction());
		} catch (Exception ex) {
			logger.error(ex);
			return ERROR;
		}
	}
	
	public String index() {
//		if(this.getReturnType()==0){
//			HttpServletRequest request = ServletActionContext.getRequest();
//			HttpServletResponse response = ServletActionContext.getResponse();
//			UserCookie userCookie = new UserCookie(request,response);
//			String scrId = userCookie.getCookie(Constant.SCR_COOKIE_KEY);
//			if(scrId!=null && !scrId.trim().equals("")){
//				this.setSid(Integer.valueOf(scrId));
//				return "resList";
//			}
//		}
		
		int cityId = 1; //沈阳的ID 暂时写死
		this.setAreaList(this.getAreaService().findAreasByCityIdIsShow(cityId, 1, "orderNum", 0));
		return "index";
	}
	
	/**
	 * 搜索
	 * @return
	 */
	public String scriptorium(){
		if(this.getAreaId()>0){
			if(this.getScrType()==0){
				this.setScrType(1);
			}
			
			this.setScriptoriumList(this.getScriptoriumService().findScriptoriumsByScrInitialsScrAnglicScrPhoneticScrTypeAreaIdIsShow(null, null, null, this.getScrType(), this.getAreaId(), 1, "orderNum", 1));
			if(this.getScriptoriumList()!=null && this.getScriptoriumList().size()>0){
				String az = "";
				String rs = "";
				boolean bl = false;
				for(int i=0;i<this.getScriptoriumList().size();i++){
					Scriptorium s = (Scriptorium)this.getScriptoriumList().get(i);
					String[] al = az.split(",");
					if(al.length>0){
						bl=true;
						if(al[0].equals("")){
							az="\""+s.getScrInitials()+"\"";
						}else{
							for(int j=0;j<al.length;j++){
								if(al[j].equals("\""+s.getScrInitials()+"\"")){
									bl=false;
								}
							}
							if(bl){
								az=az+",\""+s.getScrInitials()+"\"";
							}
						}
						
					}
					if(rs.equals("")){
						rs = "{\"id\":"+s.getId()+",\"sn\":\""+s.getScrInitials()+"\",\"name\":\""+s.getScrName()+"\"}";
					}else{
						rs = rs+","+"{\"id\":"+s.getId()+",\"sn\":\""+s.getScrInitials()+"\",\"name\":\""+s.getScrName()+"\"}";
					}
				}
				//this.setJsonString("{\"az_data\":{\"id\":\"123\",\"name\":\"JSONServlet\",\"say\":\"Hello , i am a servlet !\",\"password\":\"JSON\"},\"success\":true}");  
				this.setJsonString("{\"az_data\":["+az+"],\"reatan_data\":["+rs+"]}");
			}
		}
		return "json";
	}
	
	//显示当前位置
	public String getURL(){
		
		return "url";
	}
	
}
